package com.gs.api.util.video.parse.toutiao;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.gs.api.util.http.HttpClientUtil;
import com.gs.api.util.video.parse.ParseConstant;
import com.gs.api.util.video.parse.ParseUtil;
import com.gs.api.util.video.parse.vo.VideoVo;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.CRC32;

/**
 * @author guoyunfei on 2022/4/14 16:32
 * @className ToutiaoApp
 * @description TODO
 * @Version 1.0
 */
public class ToutiaoApp {


    /**
     * 抖音解析入口
     * @param url
     * @return 抖音图集，视频地址
     */
    public static VideoVo toutiaoParseUrl(String url) throws IOException {

        VideoVo videoVo = new VideoVo();

        String itemId = ParseUtil.resp(url).split("/video/")[1].split("/")[0];

        url = "https://m.toutiao.com/i" + itemId + "/info/v2/";

        String jsonStr = Jsoup.connect(url).ignoreContentType(true).execute().body();

        String video_id = JSONObject.parseObject(jsonStr).getJSONObject("data").getString("video_id");
        String title = JSONObject.parseObject(jsonStr).getJSONObject("data").getString("title");

        String toutiaoUrl = getToutiaoUrl(video_id);

        String dataJson = Jsoup.connect(toutiaoUrl).ignoreContentType(true).execute().body();

        System.out.println(dataJson);

        String main_url = JSONObject.parseObject(dataJson).getJSONObject("data").getJSONObject("video_list").getJSONObject("video_3").getString("main_url");

        System.out.println(main_url);

        String s = Base64.decodeStr(main_url);
        System.out.println(s);

        return videoVo;
    }

    //获取头条请求地址
    public static String getToutiaoUrl(String video_id){
        String r = ParseUtil.getRandom(16);
        CRC32 crc32 = new CRC32();

        String s = String.format(ParseConstant.TOUTIAO_VIDEO_URL, video_id, r);

        //进行crc32加密。
        crc32.update(s.getBytes());
        String crcString = crc32.getValue() + ""; //38456043
        s = s + "&s="+crcString;

        return ParseConstant.TOUTIAO_URL + s;
    }

    public static void main(String[] args) throws IOException {
        //toutiaoParseUrl(ParseUtil.filterUrl("https://m.toutiao.com/is/N3xrSUp/ 最近“广西科目三”火了!短短几天29亿播放,广西人:没听说过! - 今日头条"));

        String ddd = "aHR0cDovL3Y2LWRlZmF1bHQuaXhpZ3VhLmNvbS8zZWFmOWNjNzY2ODMzM2I3ZjQ5Yzc2NzNiMTE3ZjI0NS82MjU4ZWFmYi92aWRlby90b3MvY24vdG9zLWNuLXZlLTAwMjYtYWxpbmMyL2M5YTA2NDMwMzhhZjQ1NjA5NDliODhmMDVhMWU1ZjhmLz9hPTIwMTImYnI9MTE1NSZidD0xMTU1JmNkPTAlN0MwJTdDMSU3QzAmY2g9MCZjcj0wJmNzPTAmY3Y9MSZkcj0wJmRzPTMmZXI9JmZ0PUZJMkJNTk42Vn42d2JMTVpxOGR6SkxlT1labGNUUlJvRjJiTFJyT3pGdVptJmw9MjAyMjA0MTUxMDQzNDQwMTAyMDkxNTkwMjMwMDI1QjEyMCZscj0mbWltZV90eXBlPXZpZGVvX21wNCZuZXQ9NSZwbD0wJnFzPTAmcmM9YWpsNFpETTZabXR5UERNek5HUXpNMEFwYVdVek16TTdaRHRsTnpsb2FETTBaR2N0YTE1ZWNqUm5aMTlnTFMxa0xpOXpjeTAxTVRNMkwyRmdOalF4TWk0eU1UVTZZdyUzRCUzRCZ2bD0mdnI9";
        String s = Base64.decodeStr(ddd);
        System.out.println(s);
   }
}
